using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class CheckOut : System.Web.UI.UserControl
{
    processOrder pOrder;
    processOrderDetail pOrderDetail;
    processPaymentType pPayment;
    processDeliveryType pDelivery;
    processUser pUser;
    protected void Page_Load(object sender, EventArgs e)
    {
        pOrder = new processOrder();
        pOrderDetail = new processOrderDetail();
        pPayment = new processPaymentType();
        pDelivery = new processDeliveryType();
        pUser = new processUser();
        
        Load_gvwCart();
        Load_ddlDelivery();
        Load_ddlPayment();
        //txtOrderStatus.Text = "Undelivered"; 
        DataTable dtCart = (DataTable)Session["Cart"];
        for (int i = 0; i < dtCart.Rows.Count; i++)
        {
            string ProductID = dtCart.Rows[i]["ProductID"].ToString();
        }
        
    }

    public void Load_gvwCart()
    {
        DataTable dt = (DataTable)Session["Cart"];        
        //DataRow dr = dt.NewRow();       
        gvwCart.DataSource = dt.DefaultView;
        gvwCart.DataBind();

       
    }

    private void Load_ddlPayment()
    {
        ddlPayment.DataSource = pPayment.getPaymentType();
        ddlPayment.DataTextField = "PaymentTypeName";
        ddlPayment.DataValueField = "PaymentTypeID";
        ddlPayment.DataBind();
    }

    private void Load_ddlDelivery()
    {
        ddlDelivery.DataSource = pDelivery.getDeliveryType();
        ddlDelivery.DataTextField = "DeliveryTypeName";
        ddlDelivery.DataValueField = "DeliveryTypeID";
        ddlDelivery.DataBind();
    }
    protected void btnOrder_Click(object sender, EventArgs e)
    {
        try
        {
            if (Session["UserName"] == null)
            {
                Response.Redirect("index.aspx?subpara=login");
            }
            else
            {
                string OrderID = pOrder.getOrderID();                
                string UserID = pUser.getUserID1(Session["UserName"].ToString(), Session["Password"].ToString());
                string DeliveryTypeID = ddlDelivery.SelectedValue.ToString();
                //                
                DateTime OrderDate = DateTime.Now;
                string ReceiverName = txtReceiverName.Text;
                string ReceiverAddress = txtAddress.Text;
                string OrderStatus = "Undelivered";
                string PaymentTypeID = ddlPayment.SelectedValue.ToString();

                DataTable dtCart = (DataTable)Session["Cart"];
                if (pOrder.insertOrder(OrderID, OrderDate, ReceiverName, ReceiverAddress, OrderStatus, UserID, PaymentTypeID, DeliveryTypeID) > 0)
                {
                    Response.Write("<script>alert('Order successfull!');</script>");
                }
                    
                for (int i = 0; i < dtCart.Rows.Count; i++)
                {
                    Label lblProductQuantity = (Label)gvwCart.Rows[i].FindControl("lblQuantity");
                    lblProductQuantity.Text = dtCart.Rows[i]["Quantity"].ToString();
                    int Quantity = Int32.Parse(lblProductQuantity.Text);
                    Label lblTotal = (Label)gvwCart.Rows[i].FindControl("lblTotal");
                    lblTotal.Text = dtCart.Rows[i]["Total"].ToString();
                    float Price = Convert.ToSingle(lblTotal.Text);
                   
                    string OrderId = pOrder.getOrderIDNewest(UserID).ToString();
                    string ProductID = dtCart.Rows[i]["ProductID"].ToString();
                    txtProductID.Text = ProductID;

                    // generate 16 digit Order number by get 1 ditgit of DeliveryTypeID, 7 digit of ProductTypeID and 8 digit of OrderID
                    string OrderNumber = DeliveryTypeID.Substring(4).ToString() + ProductID + OrderID;

                    
                    if (pOrderDetail.insertOrderDetail(OrderNumber, Quantity, Price, OrderID, ProductID) > 0)
                    {
                        Response.Write("<script>alert('Order Detail successfull!');</script>");
                    }
                }
                Session.Remove("Cart");
                Response.Redirect("index.aspx");
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }
    protected void gvwCart_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblQuantity = (Label)e.Row.FindControl("lblQuantity");
            lblQuantity.Text = ((DataRowView)e.Row.DataItem)["Quantity"].ToString();
            Label lblTotal = (Label)e.Row.FindControl("lblTotal");
            lblTotal.Text = ((DataRowView)e.Row.DataItem)["Total"].ToString();                            
        }
    }
    protected void gvwCart_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvwCart.PageIndex = e.NewPageIndex;
        Load_gvwCart();
    }
}
